﻿<div id="dgToolbarBXJGCMSColumn" class="datagrid-toolbar1">
    <div class="toolbar_right">
    </div>
</div>
<table id="dgBXJGCMSColumn"></table>
<script>
    (function () {
        //若在js函数内部直接使用 razor变量，将失去vs2017提供的js代码折叠效果，因此这里搞个变量
        var m = 'BXJGCMSColumn';

        var canAdd = abp.auth.isGranted(m+ 'Create');
        var canUpdate = abp.auth.isGranted(m+ 'Update');
        var canDelete = abp.auth.isGranted(m+ 'Delete');

        var $toolbar = $('#dgToolbar' + m);
        var $toolbarRight = $toolbar.find('.toolbar_right');
        var $dg = $('#dg' + m);
        var $cmenu = $('<div />');
        var expids = new Array();//记录所有已展开的节点id

        $cmenu.menu();
        if (canAdd) {
            var $btnAdd = $('<a />').insertBefore($toolbarRight);
            var $btnAddOptions = {
                iconCls: 'icon-add',
                text: abp.localization.bxjgUtils('新增')
            };
            $btnAdd.linkbutton($.extend($btnAddOptions, {
                plain: true,
                onClick: function () {
                    var rows = $dg.treegrid('getSelections');
                    showForm(0, rows.length > 0 ? rows[0].id : $dg.treegrid('getRoot').id);
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnAddOptions, {
                onclick: function () {
                    $btnAdd.trigger('click');
                }
            }));
        }

        if (canUpdate) {
            var $btnUpdate = $('<a />').insertBefore($toolbarRight);
            var $btnUpdateOptions = {
                iconCls: 'icon-edit',
                text: abp.localization.bxjgUtils('修改')
            };
            $btnUpdate.linkbutton($.extend($btnUpdateOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return false;

                    if (!rows[0].id) {
                        abp.message.warn(abp.localization.bxjgUtils('此节点不允许修改！'));
                        return;
                    }

                    showForm(rows[0].id);
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnUpdateOptions, {
                onclick: function () {
                    $btnUpdate.trigger('click');
                }
            }));
        }

        if (canDelete) {
            var $btnDelete = $('<a />').insertBefore($toolbarRight);
            var $btnDeleteOptions = {
                iconCls: 'icon-remove',
                text: abp.localization.bxjgUtils('删除')
            };
            $btnDelete.linkbutton($.extend($btnDeleteOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.confirm(
                        abp.localization.bxjgUtils('DeleteConfirm'),
                        abp.localization.bxjgUtils('DeleteConfirmPropt'),
                        function (isConfirmed) {
                            if (isConfirmed) {
                                //这里有个诡异的问题，若用两个parent会导致刷新异常
                                abp.ui.setBusy($dg.parent().parent().parent(), abp.services.app[abp.utils.toCamelCase(m)].delete({ id: rows[0].id }).done(function (d) {
                                    abp.notify.success(abp.localization.bxjgUtils('删除') + abp.localization.bxjgUtils('成功') + '!');
                                    $dg.treegrid('unselectAll');
                                    $btnRefresh.trigger('click');
                                }));
                            }
                        }
                    );
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnDeleteOptions, {
                onclick: function () {
                    $btnDelete.trigger('click');
                }
            }));
        }

        var $btnRefresh = $('<a />').insertBefore($toolbarRight);
        $btnRefresh.linkbutton({
            iconCls: 'icon-reload',
            text: abp.localization.bxjgUtils('刷新'),
            plain: true,
            onClick: function () {
                $dg.treegrid('reload');
            }
        });
        //str:删除 审核，注意本地化
        function getSelected(str) {
            var rows = $dg.treegrid('getSelections');
            if (rows.length == 0) {//若没有选择，返回的是一个空数组，而非undifined/null
                abp.message.warn(abp.localization.bxjgUtils('请选择要{0}的行', $.trim(str)));
                return false;
            }
            return rows;
        }
        //只有新增和修改会使用编辑表单页面，批量审核或其它需要表单的操作应该另外定义方法而不是高度抽象一个表单窗口
        //em：编辑时的id 若不传则新增
        //parent 父级节点id
        function showForm(id, parent) {
            var $dlg = $('<div id="dlg' + m + '" />');
            if (id)
                $dlg.data('id', id);
            if (parent)
                $dlg.data('parent', parent);

            $dlg.data('onSaved', function () {
                $dlg.dialog('close');
                $dg.treegrid('reload');
            });

            $dlg.dialog({
                iconCls: id ? $btnUpdateOptions.iconCls : $btnAddOptions.iconCls,
                title: (id ? $btnUpdateOptions.text : $btnAddOptions.text) + '栏目',
                width: 700,
                height: 500,
                border: false,
                bodyCls: 'dlg-box',
                modal: true,
                href: '/BXJGCMS/' + 'BXJGCMSColumn' + '/form.html?_t=' + abp.pageLoadTime.getTime(),
                buttons: [{
                    iconCls: 'icon-ok',
                    text: abp.localization.bxjgUtils('保存'),
                    handler: function () {
                        var callback = $dlg.data('clickSave');
                        if (callback)
                            callback();
                    }
                }, {
                    iconCls: 'icon-undo',
                    text: abp.localization.bxjgUtils('返回'),
                    handler: function () {
                        $dlg.dialog('close');
                    }
                }],
                onClose: function () {
                    $dlg.dialog('destroy');
                }
                //可以通过onload事件初始化控件，如：下拉框
            });
        }

        $dg.treegrid({
            fit: true,
            border: false,
            idField: 'id',
            //dnd:true,这是tree的属性，这里没啥鸟用，treegrid需要扩展js
            treeField: 'text',
            rownumbers: true,
            queryParams: { loadParent: true },
            toolbar: '#dgToolbar' + m,
            url: abp.appPath + 'api/services/app/' + 'BXJGCMSColumn' +'/GetAll',
            //url: abp.apppath + 'api/services/app/BXJGCMSColumn/GetAll',
            columns: [[
                { field: 'text', title: '名称',width:400 },
                { field: 'columnTypeText', title: '栏目类型' ,width:90},
                { field: 'contentTypeDisplayName', title: '内容类型',width:90 },
                { field: 'isSysDefine', title: '系统预设',width:80, align: 'center', formatter: abp.easyui.datagridBool},
                { field: 'listTemplate', title: '列表页模板',width:100 },
                { field: 'detailTemplate', title: '详情页模板',width:100 },
                { field: 'code', title: '编号',width:200 }
            ]],
            onLoadSuccess: function (node, data) {
                $(this).treegrid('enableDnd');
                expids.forEach(function (value, index) {
                    $dg.treegrid('expand', value);
                });
            },
            onDrop: function (target, source, point) {
                switch (point) {
                    case 'top':
                        point = 0;
                        break;
                    case 'bottom':
                        point = 1;
                        break;
                    case 'append':
                        point = 2;
                        break;
                }
                abp.ui.setBusy($dg.parent().parent().parent(), abp.services.app[abp.utils.toCamelCase( m)].move({ targetId: target.id, id: source.id, moveType: point }).done(function (d) {
                    abp.notify.success(abp.localization.bxjgUtils('修改') + abp.localization.bxjgUtils('成功') + '!');
                    $btnRefresh.trigger('click');
                }));
            },
            onContextMenu: function (e, row) {
                if (!row)
                    return;
            	e.preventDefault();//禁止浏览器默认右键菜单
                //可以在这里临时创建右键菜单，则可以通过闭包访问row
                $dg.treegrid('select',row.id);
			    $cmenu.menu('show', {
				    left: e.pageX,
				    top: e.pageY
			    });
            },
            onDblClickCell:function(field,row){
                $dg.treegrid('toggle',row.id);
            },
            onExpand: function (row) {
                if ($.inArray(row.id, expids)==-1)
                    expids.push(row.id);
            },
            onCollapse: function (row) {
                expids.splice($.inArray(row.id, expids), 1);
            }
        });
     }());
</script>


